package com.cr.front.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.front.base.response.PageData;
import com.cr.front.vehicle.dao.mapper.VehicleMapper;
import com.cr.front.vehicle.pojo.entity.Vehicle;
import com.cr.front.vehicle.pojo.query.DayRentVehicleInfoQuery;
import com.cr.front.vehicle.pojo.query.MonthRentVehicleInfoQuery;
import com.cr.front.vehicle.pojo.vo.DayRentVehicleInfoVO;
import com.cr.front.vehicle.pojo.vo.VehicleIndexVO;
import com.cr.front.vehicle.pojo.vo.VehicleVO;
import com.cr.front.vehicle.pojo.vo.monthVehicleInfoVO;
import com.cr.front.vehicle.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<VehicleIndexVO> selectIndexVehicles(Character rentType) {
        List<VehicleIndexVO> vehicleIndexVO = vehicleMapper.selectIndexVehicles(rentType);
        return vehicleIndexVO;
    }

    @Override
    public PageData dayRentVehicleInfo(DayRentVehicleInfoQuery dayRentVehicleInfoQuery) {
        List<DayRentVehicleInfoVO> dayRentVehicleInfoVO = vehicleMapper.dayRentVehicleInfo(dayRentVehicleInfoQuery);
        //准备PageData对象用来装返回给前端的数据
        PageData vehicleData = new PageData();
        //向PageData对象中装存入当前页字典数据
        vehicleData.setList(dayRentVehicleInfoVO);
        //向PageData对象中装存入总记录数
        vehicleData.setTotal(vehicleMapper.selectVehicleCount(dayRentVehicleInfoQuery));



        return vehicleData;
    }

    @Override
    public monthVehicleInfoVO monthVehicleInfoVO( MonthRentVehicleInfoQuery monthRentVehicleInfoQuery) {
        monthVehicleInfoVO monthVehicleInfoVO = vehicleMapper.monthVehicleInfoVO(monthRentVehicleInfoQuery);
        return monthVehicleInfoVO;
    }

    @Override
    public VehicleVO getDetail(Long id) {
        Vehicle vehicle= vehicleMapper.selectById(id);
        VehicleVO vehicleVO=new VehicleVO();
        BeanUtils.copyProperties(vehicle,vehicleVO);
        return vehicleVO;
    }

    @Override
    public List<VehicleVO> getRecommend(Long id) {
        QueryWrapper<Vehicle> query=new QueryWrapper<>();
        query.last("order by create_time desc limit 4");
        query.notIn("id",id);
        List<Vehicle> list = vehicleMapper.selectList(query);
        List<VehicleVO> vehicleVOS=new ArrayList<>();
        for(Vehicle vehicle : list){
            VehicleVO vehicleVO=new VehicleVO();
            BeanUtils.copyProperties(vehicle,vehicleVO);
            vehicleVOS.add(vehicleVO);
        }
        return vehicleVOS;
    }
}
